# -*- coding: utf-8 -*-
"""
Created on 2021-6-6 16:06:24
客户端类定义
@author: 王韬
"""
import pygame
from pygame.locals import *
import threading

import functions as fn


# ###################<<自定义线程类>>################### #
class MyThread(threading.Thread):
    """

    Attributes:
        func -- target function name without '()'
        *args -- arguments of func
    Methods:
    """
    def __init__(self, name, func, *args):
        super().__init__()
        self.name = name
        self.func = func
        self.args = args

    def run(self):
        self.func(*self.args)
        print('Thread:', self.name, 'start')


# ###################<<pygame 类>>################### #
class Button(pygame.sprite.Sprite):
    """Button class -> pygame.Surface

    Attributes:
        name -- the name of button, string
        pos -- the position of button, (x, y)
        size -- the size of button, (x, y)
    Keyword arguments:
        style -- the style of button, e.g. 'white_grey'
    Methods:
    """
    def __init__(self, name, pos, size, **kwargs):
        super().__init__()
        self.name = name
        self.pos = pos
        self.surface = pygame.Surface(size)
        self.size = size
        
        self.font = pygame.font.Font(None, 36)
        self.fill_color = (60, 60, 61)
        self.txt_color = (66, 156, 227)
        self.txt = self.font.render(self.name, True, self.txt_color)
        self.txt_x = size[0] // 2 - self.txt.get_width() // 2
        self.txt_y = size[1] // 2 - self.txt.get_height() // 2

        self.update_btn()

    def check_click(self, pos):
        """检测指针位置是否位于button上

        Arguments:
            pos -- position of mouse, (x, y)
        """

        x_match = self.pos[0] < pos[0] < self.pos[0] + self.size[0]
        y_match = self.pos[1] < pos[1] < self.pos[1] + self.size[1]

        if x_match and y_match:
            return True, self.name
        else:
            return False, self.name

    def update_btn(self):
        self.txt = self.font.render(self.name, True, self.txt_color)
        self.surface.fill(self.fill_color)
        self.surface.blit(self.txt, (self.txt_x, self.txt_y))

    def on_click(self):
        self.txt_color = (66, 156, 227)
        self.fill_color = (62, 63, 63)
        self.update_btn()

    def off_click(self):
        self.txt_color = (255, 255, 255)
        self.fill_color = (40, 41, 42)
        self.update_btn()


class Txt(pygame.sprite.Sprite):
    """显示文本

    Attributes:
        text -- string type
        size -- size of background surface, (w, h)
        font -- pygame Font()
        bg_clr -- bg fill color

        bg -- Surface()
    """
    def __init__(self, text, size, font, bg_clr):
        super().__init__()
        self.font = font
        self.text = text
        self.size = size

        self.bg = pygame.Surface(size)
        self.image = self.bg
        self.rect = self.bg.get_rect()

        clr_default = (203, 142, 59)
        pos_default = (0, 0)
        self.set(clr_default, self.text, bg_clr, pos_default)

    def set(self, txt_clr, txt, bg_clr, pos):
        """set text"""

        self.bg.fill(bg_clr)
        txt = self.font.render(txt, True, txt_clr)
        self.text = txt
        txt_w, txt_h = txt.get_size()
        bg_w, bg_h = self.bg.get_size()
        self.bg.blit(txt, (bg_w//2 - txt_w//2, bg_h//2 - txt_h//2))

        self.image = self.bg
        self.rect = self.bg.get_rect()
        self.rect.left = pos[0]
        self.rect.top = pos[1]


class CardDict:
    """卡牌库

    Attributes:
        cards -- dictionary of cards by keys below

    ********** defender cards **********
        regenerate0 -- regenerate card with ad:10
        regenerate1 -- regenerate card with ad:1
        regenerate2 -- regenerate card with ad:2
        regenerate3 -- regenerate card with ad:3

    ********** attacker cards **********
        attack0 -- attack card with ad:10
        attack1 -- attack card with ad:1
        attack2 -- attack card with ad:2
        attack3 -- attack card with ad:3

    Methods:
    """

    # 定义构造方法
    def __init__(self):
        self.att_num = 4
        self.def_num = 4

    @staticmethod
    def get_card(card_name, *card_attr):

        if card_name == 'attack':
            return AttackCard(*card_attr)
        elif card_name == 'regenerate':
            return RegenerateCard(*card_attr)


class Card(pygame.sprite.Sprite):
    """Card basic class


    """
    def __init__(self, image='card_default.png'):
        super().__init__()
        self.image, self.rect = fn.load_img(image)
        self.index = 0
        self.selected = False


class AttackCard(Card):
    """攻击牌

    Attributes:
        ad -- attack damage
    Methods:
    """
    def __init__(self, ad):
        super().__init__(image='card_attack'+str(ad)+'.png')
        self.ad = ad

        font = pygame.font.Font(None, 50)
        txt_ad = font.render(str(self.ad), True, (255, 0, 0))
        self.image.blit(txt_ad, self.rect)


class RegenerateCard(Card):
    """回复

    Attributes:
        ra -- regenerate amount
    Methods:
    """

    def __init__(self, ra):
        super().__init__(image='card_regenerate'+str(ra)+'.png')
        self.ra = ra

        font = pygame.font.Font(None, 50)
        txt_ad = font.render(str(self.ra), True, (0, 255, 0))
        self.image.blit(txt_ad, self.rect)


